package com.miaomiaojia.android;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.miaomiaojia.bll.CommonTool;
import com.miaomiaojia.control.BtnPrice;
import com.miaomiaojia.control.MyTabHost;
import com.miaomiaojia.entity.MyTabHostArge;
import com.miaomiaojia.entity.Price;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.ActivityGroup;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint("HandlerLeak")
public class DetailActivity extends ActivityGroup {

	private String title;
	private Bitmap img = null;
	private List<Price> priceList;

	private Handler favoriteHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Intent intent = new Intent(DetailActivity.this,
					BrowserActivity.class);
			startActivity(intent);
		}
	};
	private Handler shareHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Intent intent = new Intent(DetailActivity.this,
					BrowserActivity.class);
			startActivity(intent);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);

		((Button) findViewById(R.id.btnback))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						DetailActivity.this.finish();
					}
				});

		List<Intent> listIntents = new ArrayList<Intent>();
		listIntents.add(new Intent(this, IndexActivity.class));
		listIntents.add(new Intent(this, BrowserActivity.class));
		List<Handler> listHandlers = new ArrayList<Handler>();
		listHandlers.add(favoriteHandler);
		listHandlers.add(shareHandler);
		MyTabHostArge arge = new MyTabHostArge(R.layout.tabhost_detail, false,
				listIntents, false, listHandlers);

		MyTabHost myTabHost = (MyTabHost) findViewById(R.id.mytabhost);
		myTabHost.setMyTabHostArge(arge);
		myTabHost.setTabInit();

		new Thread() {
			@Override
			public void run() {
				getData();
				handler.sendEmptyMessage(0);
			}
		}.start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_detail, menu);
		return true;
	}

	private void getData() {
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			int id = bundle.getInt("id");
			if (id > 0) {
				String url = getResources().getString(R.string.host)
						+ getResources().getString(R.string.detail);
				url = String.format(url, id);
				String result = CommonTool.getResult(url);
				JSONObject data;
				try {
					data = new JSONObject(result);
					int status = data.getInt("status");
					if (status == 1) {
						JSONObject produce = new JSONObject(
								data.getString("data"));
						img = CommonTool.getHttpBitmap(produce.getString("img_url"));
						title = produce.getString("title");
						JSONArray priceArray = produce
								.getJSONArray("compare_price");
						priceList = new ArrayList<Price>();
						for (int i = 0; i < priceArray.length(); i++) {
							JSONObject pricejson = priceArray.getJSONObject(i);
							Price price = new Price();
							price.setPrice(pricejson.getLong("price"));
							price.setTitle(pricejson.getString("shop_name"));
							price.setUrl(pricejson.getString("url"));
							priceList.add(price);
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			try {
				LinearLayout layout = (LinearLayout) findViewById(R.id.linear1);
				ImageView ivImageView = (ImageView) layout
						.findViewById(R.id.imgproduce);
				TextView tView = (TextView) layout
						.findViewById(R.id.titleproduce);
				ivImageView.setImageBitmap(img);
				tView.setText(title);

				for (int i = 0; i < priceList.size(); i++) {
					Price price = priceList.get(i);
					LinearLayout priceLinearLayout = (LinearLayout) LayoutInflater
							.from(DetailActivity.this).inflate(
									R.layout.pricebutton, null);
					BtnPrice btnpriceButton = (BtnPrice) priceLinearLayout
							.findViewById(R.id.btnprice);
					priceLinearLayout.removeView(btnpriceButton);
					btnpriceButton.setPrice(price);
					layout.addView(btnpriceButton);
				}

				// ScrollView sv = (ScrollView) layout
				// .findViewById(R.id.scrollView1);
				// MyTabHost mTabHost=(MyTabHost) layout
				// .findViewById(R.id.mytabhost);
				// sv.setLayoutParams(new
				// LayoutParams(LayoutParams.MATCH_PARENT,
				// LayoutParams.WRAP_CONTENT));
				// LayoutParams lParams=new
				// LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);

			} catch (Exception e) {
				e.printStackTrace();
				// TextView tView = (TextView) findViewById(R.id.titleView1);
				// tView.setText(e.getMessage());
			}
		}
	};
}
